﻿using Lanbt.Core.Entity.Dto;
using Lanbt.Core.Entity.Dto.lanbt_power;
using Lanbt.Core.Entity.Dto.lanbt_role;
using Lanbt.Core.Entity.Dto.lanbt_system_config;
using Lanbt.Core.Entity.Dto.lanbt_user;
using Lanbt.Core.Entity.EntityDB;
using Lanbt.Core.Service.SqlService.IService;
using Lanbt.Core.Utility;
using Lanbt.Core.Web.Controllers.Base;
using Lanbt.Core.Web.Filter;
using Lanbt.Core.Web.Filter.Validate;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Lanbt.Core.Web.Controllers.Action
{
    /// <summary>
    /// 系统管理
    /// </summary>
    public class SystemManagerAction : ActionController
    {
        private readonly IRoleService _roleService;
        private readonly IUserService _userService;
        private readonly IPowerService _powerService;
        public readonly IDatabaseService _databaseService;

        public SystemManagerAction(IRoleService roleService, IUserService userService, IPowerService powerService, IDatabaseService databaseService)
        {
            _roleService = roleService;
            _userService = userService;
            _powerService = powerService;
            _databaseService = databaseService;
        }

        #region User

        #region 查询

        /// <summary>
        /// 获取登录人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.user.info.get")]
        public async Task<ResultOutput<GetUserInfo>> GetUserInfo()
        {
            return await _userService.GetUserInfo(CurrentUser.Id);
        }

        /// <summary>
        /// 生成用户角色下拉框
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.users.roleids.query")]
        public async Task<ResultOutput<IList<XM_SelectOutput>>> QueryRolesByUserId(string userId)
        {
            return await _roleService.QueryRolesByUserId(userId);
        }

        /// <summary>
        /// 分页查询用户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.users.list.pages.query")]
        public async Task<PagesResultOutput<QueryUsersOutput>> QueryPagesUsers(QueryUsersInput input)
        {
            return await _userService.QueryPagesUsers(input);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/login.input")]
        [CustomAllowAnonymous]
        public async Task<ResultOutput<string>> Input(LoginInput input)
        {
            return await _userService.Input(input);
        }

        #endregion 查询

        #region 增删改

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/lanbt.system.manager.users.insert")]
        public async Task<ResultOutput<string>> AddUser([FromBody] InsertUserInput input)
        {
            return await _userService.InsertUser(input);
        }

        /// <summary>
        /// 删除单个用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.users.one.delete")]
        public async Task<ResultOutput<string>> DeleteOne([Required(ErrorMessage = "请选择用户")] [FromBody] string id)
        {
            var result = new ResultOutput<string>("删除成功");
            await _userService.DeleteById(id);
            return result;
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.users.batch.delete")]
        public async Task<ResultOutput<string>> BatchDelete([ArrayNonzero(ErrorMessage = "请选择用户")] [FromBody] List<string> ids)
        {
            var result = new ResultOutput<string>("删除成功");
            await _userService.DeleteByIds(ids);
            return result;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.users.update")]
        public async Task<ResultOutput<string>> UpdateUser([FromBody] UpdateUserInput input)
        {
            var result = new ResultOutput<string>("修改成功");
            await _userService.Update(input);
            return result;
        }

        /// <summary>
        /// 修改是否是超管状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.users.isadmin.update")]
        public async Task<ResultOutput<string>> UpdateIsAdminById([Required(ErrorMessage = "用户ID不能为空")] [FromBody] string id)
        {
            return await _userService.UpdateIsAdminById(id);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.user.password.update")]
        public async Task<ResultOutput> UpdatePassword([FromBody] UpdatePasswordInput input)
        {
            return await _userService.UpdatePassword(input);
        }

        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.users.roleids.update")]
        public async Task<ResultOutput<string>> UpdateRoleIdsById([FromBody] UpdateRoleIdsByIdInput input)
        {
            return await _userService.UpdateRoleIdsById(input);
        }

        #endregion 增删改

        #endregion User

        #region Role

        #region 查询

        /// <summary>
        /// 分页查询角色列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.role.list.pages.query")]
        public async Task<PagesResultOutput<lanbt_role>> QueryPagesRole(QueryPagesRoleInput input)
        {
            return await _roleService.QueryPagesRole(input);
        }

        /// <summary>
        /// 获取角色权限树形数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.role.power,tree.query")]
        public async Task<ResultOutput<IList<TreeData>>> QueryRolePowerTreedata(string id)
        {
            return await _roleService.QueryRolePowerTreedata(id);
        }

        #endregion 查询

        #region 增删改

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost("/lanbt.system.manager.role.insert")]
        public async Task<ResultOutput<string>> Insert([Required(ErrorMessage = "角色名不能为空")] [FromBody] string name)
        {
            return await _roleService.Insert(name);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.role.one.delete")]
        public async Task<ResultOutput<string>> DeleteById([Required(ErrorMessage = "角色ID不能为空")][FromBody] string id)
        {
            return await _roleService.DeleteById(id);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.role.batch.delete")]
        public async Task<ResultOutput<string>> DeleteByIds([ArrayNonzero(ErrorMessage = "请选择角色")] [FromBody] List<string> ids)
        {
            return await _roleService.DeleteByIds(ids);
        }

        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.role.update")]
        public async Task<ResultOutput<string>> Update([FromBody] UpdateRoleInput input)
        {
            return await _roleService.Update(input);
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.role.assign.power.update")]
        public async Task<ResultOutput> UpdatePowerIdsById([FromBody] UpdatePowerIdsByIdInput input)
        {
            return await _roleService.UpdatePowerIdsById(input);
        }


        #endregion 增删改

        #endregion Role

        #region Power

        #region 查询

        /// <summary>
        /// 通过ID获取单个权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.power.byid.get")]
        public async Task<ResultOutput<lanbt_power>> GetPowerById([Required(ErrorMessage = "主键不能为空")] string id)
        {
            return await _powerService.GetPowerById(id);
        }

        /// <summary>
        /// 分页获取权限列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.power.paged.query")]
        public async Task<PagesResultOutput<QueryPagedPowerListOutput>> QueryPagedPowerList(QueryPagedPowerListInput input)
        {
            return await _powerService.QueryPagedPowerList(input);
        }

        /// <summary>
        /// 获取父级选择框数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.power.select.query")]
        public async Task<ResultOutput<List<XM_SelectOutput>>> QuerySelectList()
        {
            return await _powerService.QuerySelectList();
        }

        /// <summary>
        /// 获取菜单目录
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.system.manager.power.menulist.query")]
        public async Task<ResultOutput<List<QueryMenuListOutput>>> QueryMenuList()
        {
            return await _powerService.QueryMenuList();
        }

        #endregion 查询

        #region 增删改

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/lanbt.system.manager.power.insert")]
        public async Task<ResultOutput> InsertPower([FromBody] InsertPowerInput input)
        {
            return await _powerService.InsertPower(input);
        }

        /// <summary>
        /// 通过ID单个删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.power.one.delete")]
        public async Task<ResultOutput> DeletePowerById([Required(ErrorMessage = "主键不能为空")][FromBody] string id)
        {
            return await _powerService.DeletePowerById(id);
        }

        /// <summary>
        /// 通过Ids批量删除权限
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.system.manager.power.batch.delete")]
        public async Task<ResultOutput> DeletePowerByIds([ArrayNonzero(ErrorMessage = "请选择权限")][FromBody] string[] ids)
        {
            return await _powerService.DeletePowerByIds(ids);
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.power.update")]
        public async Task<ResultOutput> UpdatePower([FromBody] UpdatePowerInput input)
        {
            return await _powerService.UpdatePower(input);
        }

        /// <summary>
        /// 菜单隐藏或显示
        /// </summary>
        /// <remarks>菜单类型为【文件夹、页面】可以设置，【按钮、权限】不可设置</remarks>
        /// <param name="id">权限ID</param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.power.ismenushow.update")]
        public async Task<ResultOutput> UpdateMenuShowOrHide([FromBody] string id)
        {
            return await _powerService.UpdateMenuShowOrHide(id);
        }

        /// <summary>
        /// 菜单启用或禁用
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns></returns>
        [HttpPut("/lanbt.system.manager.power.isenable.update")]
        public async Task<ResultOutput> UpdateMenuEnable([FromBody] string id)
        {
            return await _powerService.UpdateMenuEnable(id);
        }

        #endregion 增删改

        #endregion Power

        #region DataBase

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <returns></returns>
        [HttpPost("/lanbt.system.manager.database.create")]
        [CustomAllowAnonymous]
        public async Task<ResultOutput<bool>> CreateDataBase()
        {
            return await _databaseService.CreateDatabase();
        }

        /// <summary>
        /// 创建表
        /// </summary>
        /// <returns></returns>
        [CustomAllowAnonymous]
        [HttpPost("/lanbt.system.manager.database.tables.create")]
        public async Task<ResultOutput<bool>> CreateTables()
        {
            return await _databaseService.CreateTables();
        }

        /// <summary>
        /// 添加超级管理员
        /// </summary>
        /// <returns></returns>
        [CustomAllowAnonymous]
        [HttpPost("/lanbt.system.manager.database.admin.add")]
        public async Task<ResultOutput<bool>> AddAdmin()
        {
            return await _userService.AddAdmin();
        }

        #endregion

    }
}
